package com.csun.cmny.provider.model.domain;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;

@Data
@Table(name = "cn_omc_point_card_account")
public class OmcPointCardAccount {
    /**
     * 卡设备id
     */
    @Id
    @Column(name = "device_id")
    private Long deviceId;

    /*
    * 状态 1-正常 2-冻结 3-已退 4-作废
    * */
    private Integer status;

    /**
     * 累计积分
     */
    @Column(name = "total_score")
    private Integer totalScore;

    /**
     * 剩余积分
     */
    @Column(name = "residual_score")
    private Integer residualScore;

    /**
     * 清空积分
     */
    @Column(name = "clear_score")
    private Integer clearScore;

    /**
     * 累计充值金额
     */
    private BigDecimal total;

    /**
     * 消费总金额
     */
    private BigDecimal monetary;

    /**
     * 退款总金额
     */
    private BigDecimal refund;

    /**
     * 当前余额
     */
    private BigDecimal remainder;

    /**
     * 创建人
     */
    private String creator;

    /**
     * 创建人ID
     */
    @Column(name = "creator_id")
    private Long creatorId;

    /**
     * 创建时间
     */
    @Column(name = "created_time")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdTime;

    /**
     * 最近操作人
     */
    @Column(name = "last_operator")
    private String lastOperator;

    /**
     * 最后操作人ID
     */
    @Column(name = "last_operator_id")
    private Long lastOperatorId;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    public void setUpdateInfo(LoginAuthDto user) {

        if (StringUtils.isBlank(creator)) {
            this.creatorId = (this.lastOperatorId = user.getUserId());
            this.creator = user.getUserName();
            this.createdTime = (this.updateTime = new Date());
        }
        this.lastOperatorId = user.getUserId();
        this.lastOperator = user.getUserName() == null ? user.getLoginName() : user.getUserName();
        this.updateTime = new Date();
    }

}